package com.cn.demo.controller;

import com.cn.demo.entity.Curriculum;
import com.cn.demo.entity.Evaluate;
import com.cn.demo.entity.Organization;
import com.cn.demo.service.*;
import com.cn.demo.utils.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpRequest;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/teacher/operation")
public class TeacherOperationController {
    @Autowired
    private CurriculumService curriculumService;

    @Autowired
    private OrganizationService organizationService;

    @Autowired
    private TeachingService teachingService;

    @Autowired
    private EvaluateService evaluateService;

    @Autowired
    private InternshipService internshipService;

    @Autowired
    private StudentService studentService;

    @Autowired
    private TeacherService teacherService;

    @Autowired
    private WaitJoinCurriculumService waitJoinCurriculumService;

    /**
     * 添加课程
     *
     * @param curriculum
     * @return
     */
    @PostMapping("/addCurriculum")
    public Result addCurriculum(@RequestBody Curriculum curriculum) {
        return curriculumService.add(curriculum);
    }

    /**
     * 修改课程
     *
     * @param curriculum
     * @return
     */
    @PutMapping("/upCurriculum")
    public Result upCurriculum(@RequestBody Curriculum curriculum) {
        return curriculumService.update(curriculum);
    }

    /**
     * 查询课程
     *
     * @param page
     * @param limit
     * @param teacherId
     * @param curriculumId
     * @param curriculumName
     * @param content
     * @param classHour
     * @param startDate
     * @param endDate
     * @param curriculumDesignId
     * @param curriculumExperimentId
     * @param curriculumInternshipId
     * @return
     */
    @GetMapping(value = "/getCurriculum")
    public Result get(@RequestParam(value = "page", defaultValue = "1") Integer page, @RequestParam(value = "limit", defaultValue = "5") Integer limit,
                      @RequestParam(value = "teacherId", required = true) String teacherId,
                      @RequestParam(value = "curriculumId", required = false) String curriculumId,
                      @RequestParam(value = "curriculumName", required = false) String curriculumName,
                      @RequestParam(value = "content", required = false) String content,
                      @RequestParam(value = "classHour", required = false) String classHour,
                      @RequestParam(value = "startDate", required = false) Date startDate,
                      @RequestParam(value = "endDate", required = false) Date endDate,
                      @RequestParam(value = "curriculumDesignId", required = false) String curriculumDesignId,
                      @RequestParam(value = "curriculumExperimentId", required = false) String curriculumExperimentId,
                      @RequestParam(value = "curriculumInternshipId", required = false) String curriculumInternshipId
    ) {
        Curriculum curriculum = new Curriculum();
        curriculum.setTeacherId(teacherId);
        curriculum.setCurriculumId(curriculumId);
        curriculum.setCurriculumName(curriculumName);
        return curriculumService.get(page, limit, curriculum);
    }


    @GetMapping(value = "/getCurriculumByCurriculumId/{curriculumId}")
    public Result get(@PathVariable("curriculumId") String curriculumId
    ) {
        return curriculumService.getByCurriculumId(curriculumId);
    }

    @PostMapping("/addOrganization")
    public Result addCurriculum(@RequestBody Organization organization) {
        return organizationService.add(organization);
    }

    @GetMapping(value = "/getOrganizationByCurriculumId")
    public Result getOrganizationByCurriculumId(@RequestParam(value = "page", defaultValue = "1") Integer page, @RequestParam(value = "limit", defaultValue = "5") Integer limit,
                                                @RequestParam(value = "curriculumId", required = true) String curriculumId,
                                                @RequestParam(value = "organizationId", required = false) String organizationId,
                                                @RequestParam(value = "organizationName", required = false) String organizationName
    ) {
        Organization organization = new Organization();
        organization.setOrganizationId(organizationId);
        organization.setOrganizationName(organizationName);
        organization.setCurriculumId(curriculumId);
        return organizationService.getOrganizationByCurriculumId(page, limit, organization);
    }

    @PutMapping("/upOrganization")
    public Result upOrganization(@RequestBody Organization organization) {
        return organizationService.update(organization);
    }

    @GetMapping(value = "/getOrganizationByOrganizationId/{organizationId}")
    public Result getOrganizationByOrganizationId(@PathVariable("organizationId") String organizationId
    ) {
        return organizationService.getByOrganizationId(organizationId);
    }


    /**
     * 删除某个班级
     *
     * @param organizationIdList
     * @return
     */
    @PostMapping(value = "/organization/delete")
    public Result delete(@RequestBody String[] organizationIdList) {
        if (organizationIdList.length == 0) {
            return Result.error();
        }
        return organizationService.delete(organizationIdList);
    }

    /**
     * 添加学生到班级中
     *
     * @param map
     * @return
     */
    @PostMapping(value = "/student/add")
    public Result organizationAddStudent(@RequestBody Map<String, Object> map) {
        String studentId = (String) map.get("studentId");
        String organizationId = (String) map.get("organizationId");
        return teachingService.organizationAddStudent(studentId, organizationId);
    }

    /**
     * 查询该班级的学生
     *
     * @param page
     * @param limit
     * @param organizationId
     * @param studentId
     * @param studentName
     * @return
     */
    @GetMapping(value = "/getOrganizationStudentByOrganizationId")
    public Result getOrganizationStudentByOrganizationId(@RequestParam(value = "page", defaultValue = "1") Integer page, @RequestParam(value = "limit", defaultValue = "5") Integer limit,
                                                         @RequestParam(value = "organizationId", required = true) String organizationId,
                                                         @RequestParam(value = "studentId", required = false) String studentId,
                                                         @RequestParam(value = "studentName", required = false) String studentName
    ) {
        return teachingService.getOrganizationStudentByOrganizationId(page, limit, organizationId, studentId, studentName);
    }

    /**
     * @param page
     * @param limit
     * @param organizationId
     * @param experimentId
     * @param studentId
     * @param studentName
     * @return
     */
    @GetMapping(value = "/getOrganizationStudentExperimentEvaluate")
    public Result getOrganizationStudentExperimentEvaluate(@RequestParam(value = "page", defaultValue = "1") Integer page, @RequestParam(value = "limit", defaultValue = "5") Integer limit,
                                                           @RequestParam(value = "organizationId", required = true) String organizationId,
                                                           @RequestParam(value = "experimentId", required = true) String experimentId,
                                                           @RequestParam(value = "studentId", required = false) String studentId,
                                                           @RequestParam(value = "studentName", required = false) String studentName
    ) {
        return evaluateService.getOrganizationStudentExperimentEvaluate(page, limit, organizationId, experimentId, studentId, studentName);
    }

    /**
     * 查询课程设计
     *
     * @param page
     * @param limit
     * @param organizationId
     * @param curriculumDesignId
     * @return
     */
    @GetMapping(value = "/getOrganizationStudentDesignEvaluate")
    public Result getOrganizationStudentDesignEvaluate(@RequestParam(value = "page", defaultValue = "1") Integer page, @RequestParam(value = "limit", defaultValue = "5") Integer limit,
                                                       @RequestParam(value = "organizationId", required = true) String organizationId,
                                                       @RequestParam(value = "curriculumDesignId", required = true) String curriculumDesignId,
                                                       @RequestParam(value = "studentId", required = false) String studentId,
                                                       @RequestParam(value = "studentName", required = false) String studentName
    ) {
        return evaluateService.getOrganizationStudentDesignEvaluate(page, limit, organizationId, curriculumDesignId, studentId, studentName);
    }


    /**
     * 查询课程设计
     *
     * @param page
     * @param limit
     * @param organizationId
     * @return
     */
    @GetMapping(value = "/getOrganizationStudentDesignTeamEvaluate")
    public Result getOrganizationStudentDesignTeamEvaluate(@RequestParam(value = "page", defaultValue = "1") Integer page, @RequestParam(value = "limit", defaultValue = "5") Integer limit,
                                                           @RequestParam(value = "organizationId", required = true) String organizationId,
                                                           @RequestParam(value = "curriculumDesignId", required = true) String curriculumDesignId,
                                                           @RequestParam(value = "designId", required = false) String designId,
                                                           @RequestParam(value = "subject", required = false) String subject
    ) {
        return evaluateService.getOrganizationStudentDesignTeamEvaluate(page, limit, organizationId,curriculumDesignId, designId, subject);
    }


    /**
     * 课程实习查询
     *
     * @param page
     * @param limit
     * @param organizationId
     * @param curriculumInternshipId
     * @return
     */
    @GetMapping(value = "/getOrganizationStudentInternshipEvaluate")
    public Result getOrganizationStudentInternshipEvaluate(@RequestParam(value = "page", defaultValue = "1") Integer page, @RequestParam(value = "limit", defaultValue = "5") Integer limit,
                                                           @RequestParam(value = "organizationId", required = true) String organizationId,
                                                           @RequestParam(value = "curriculumInternshipId", required = true) String curriculumInternshipId,
                                                           @RequestParam(value = "studentId", required = false) String studentId,
                                                           @RequestParam(value = "studentName", required = false) String studentName
    ) {
        return evaluateService.getOrganizationStudentInternshipEvaluate(page, limit, organizationId, curriculumInternshipId, studentId, studentName);
    }

    /**
     * 查询日志
     *
     * @param page
     * @param limit
     * @param internshipId
     * @return
     */
    @GetMapping(value = "/getOrganizationStudentInternshipLog")
    public Result getOrganizationStudentInternshipLog(@RequestParam(value = "page", defaultValue = "1") Integer page, @RequestParam(value = "limit", defaultValue = "5") Integer limit,
                                                      @RequestParam(value = "internshipId", required = true) String internshipId
    ) {
        return internshipService.getOrganizationStudentInternshipLog(page, limit, internshipId);
    }

    /**
     * 删除学生
     */
    @PostMapping(value = "/organizationStudentDelete")
    public Result organizationStudentDelete(@RequestBody Map<String, Object> map) {
        String organizationId = (String) map.get("organizationId");
        List<String> studentIdList = (List<String>) map.get("studentIdList");
        if (studentIdList.size() == 0) {
            return Result.error();
        }
        return teachingService.organizationStudentDelete(studentIdList, organizationId);
    }


    /**
     * 打分
     */
    @PostMapping(value = "/evaluateByStudentByCurriculumType")
    public Result evaluateByStudentByCurriculumType(@RequestBody Evaluate evaluate) {
        return evaluateService.evaluateByStudentByCurriculumType(evaluate);
    }

    /**
     * 获得个人信息
     * @param teacherId
     * @return
     */
    @GetMapping(value = "/info")
    public Result info(@RequestParam("teacherId") String teacherId){
        return teacherService.getByTeacherId(teacherId);
    }


    @PostMapping(value = "/modifyPassword")
    public Result modifyPassword(@RequestBody Map<String,Object> map) {
        String oldPassword = (String) map.get("oldPassword");
        String affirmPassword = (String) map.get("affirmPassword");
        String teacherId = (String) map.get("teacherId");
        return teacherService.modifyPassword(teacherId,oldPassword,affirmPassword);
    }

    /**
     * 显示该课程加入课程的请求同学
     */

    @GetMapping(value = "/getRequestStudent")
    public Result getRequestStudent(@RequestParam(value = "page", defaultValue = "1") Integer page, @RequestParam(value = "limit", defaultValue = "5") Integer limit,
                                    @RequestParam(value = "curriculumId", required = true) String curriculumId){
        return  waitJoinCurriculumService.getRequestStudent(page,limit,curriculumId);
    }

    @PostMapping(value = "/division")
    public Result division(@RequestBody Map<String,Object> map){
        String studentId = (String)map.get("studentId");
        String organizationId = (String)map.get("organizationId");
        return teachingService.organizationAddStudent(studentId, organizationId);
    }


}
